﻿using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Types;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.WorksheetElements
{
    [XmlRoot("cfRule", Namespace = Constants.ExcelMain)]
    public class CfRule : XmlSerializable
    {
        #region Attributes

        [XmlAttribute("priority", Form = XmlSchemaForm.Qualified)]
        public int Priority { get; set; }

        [Optional]
        [XmlAttribute("type", Form = XmlSchemaForm.Qualified)]
        public Nullable<ST_CfType> Type { get; set; }

        [Optional]
        [XmlAttribute("dxfId", Form = XmlSchemaForm.Qualified)]
        public Nullable<uint> DifferentialFormattingId { get; set; }

        [Optional]
        [XmlAttribute("stopIfTrue", Form = XmlSchemaForm.Qualified)]
        public Nullable<bool> StopIfTrue { get; set; }

        [Optional]
        [XmlAttribute("aboveAverage", Form = XmlSchemaForm.Qualified)]
        public Nullable<bool> AboveAverage { get; set; }

        [Optional]
        [XmlAttribute("percent", Form = XmlSchemaForm.Qualified)]
        public Nullable<bool> Percent { get; set; }

        [Optional]
        [XmlAttribute("bottom", Form = XmlSchemaForm.Qualified)]
        public Nullable<bool> Bottom { get; set; }

        [Optional]
        [XmlAttribute("operator", Form = XmlSchemaForm.Qualified)]
        public Nullable<ST_ConditionalFormattingOperator> Operator { get; set; }

        [Optional]
        [XmlAttribute("text", Form = XmlSchemaForm.Qualified)]
        public string Text { get; set; }

        [Optional]
        [XmlAttribute("timePeriod", Form = XmlSchemaForm.Qualified)]
        public Nullable<ST_TimePeriod> TimePeriod { get; set; }

        [Optional]
        [XmlAttribute("rank", Form = XmlSchemaForm.Qualified)]
        public Nullable<uint> Rank { get; set; }

        [Optional]
        [XmlAttribute("stdDev", Form = XmlSchemaForm.Qualified)]
        public Nullable<int> StdDev { get; set; }

        [Optional]
        [XmlAttribute("equalAverage", Form = XmlSchemaForm.Qualified)]
        public Nullable<bool> EqualAverage { get; set; }

        #endregion

        #region Elements

        [Optional]
        [XmlElement("formula", Namespace = Constants.ExcelMain)]
        public XmlList<FormulaFormatting> Formula { get; set; }

        [Optional]
        [XmlElement("colorScale", Namespace = Constants.ExcelMain)]
        public ColorScale ColorScale { get; set; }

        [Optional]
        [XmlElement("dataBar", Namespace = Constants.ExcelMain)]
        public DataBar DataBar { get; set; }

        [Optional]
        [XmlElement("iconSet", Namespace = Constants.ExcelMain)]
        public IconSet IconSet { get; set; }

        [Optional]
        [XmlElement("extLst", Namespace = Constants.ExcelMain)]
        public ExtensionList ExtensionList { get; set; }

        #endregion

        public CfRule() 
        {
            Formula = new XmlList<FormulaFormatting>();
            Formula.Changing += Formula_Changing;
        }

        void Formula_Changing(object sender, ListEventArgs<FormulaFormatting> args)
        {
            if (Formula.Count == 3)
            {
                args.Canceled = true;
            }
        }
    }
}
